package com.etc;

import java.sql.*;

    public class DbUtil2 {
        private static Connection connection=null;
        private static PreparedStatement preparedStatement=null;
        private static ResultSet resultSet=null;

        static {
            try {
                Class.forName("oracle.jdbc.driver.OracleDriver");
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }finally {
                close();
            }
        }
        public static Connection getConnection(){
            try {
                connection= DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","scott","tiger");
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return connection;
        }


        public static <T>  int executeUpdate(T[] t){
            connection=getConnection();
            int result=0;
            try {
                String sql=null;
                preparedStatement=connection.prepareStatement(sql);
                //将？替换成param
                Object[] params= (Object[]) t;
                for (int i = 0; i<params.length; i++){
                    preparedStatement.setObject(i+1,params[i]);
                }
                result = preparedStatement.executeUpdate();
            } catch (SQLException e) {
                e.printStackTrace();
            }finally {
                close();
            }
            return result;
        }
        /**
         * 关闭资源
         */
        public static void close(){
            try{
                if (null!=resultSet){
                    resultSet.close();
                }
                if (null!=preparedStatement){
                    preparedStatement.close();
                }
                if (null!=connection){
                    connection.close();
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

